package com.xiye.system.controller.buttonrights;

import com.xiye.common.controller.BaseController;
import com.xiye.common.entity.Page;
import com.xiye.common.entity.PageData;
import com.xiye.common.entity.Result;
import com.xiye.common.entity.Role;
import com.xiye.common.util.Const;
import com.xiye.common.util.Jurisdiction;
import com.xiye.common.util.ResultUtil;
import com.xiye.common.util.Tools;
import com.xiye.system.config.YmlConfig;
import com.xiye.system.service.button.ButtonService;
import com.xiye.system.service.buttonrights.ButtonrightsService;
import com.xiye.system.service.role.RoleService;
import io.swagger.annotations.*;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title ButtonrightsController.java
 * @Package com.xiye.system.controller.buttonrights
 * @Description 按钮权限
 * @Author 西野
 * @Date 2018-09-17
 * @Version V1.0
 */
@RestController
@Api(value = "/buttonrights", tags = "按钮权限")
@RequestMapping(value = "/buttonrights")
public class ButtonrightsController extends BaseController {

    String menuUrl = Const.MENU_BUTTONRIGHTS; // 菜单地址(权限用)
    @Autowired
    private ButtonrightsService buttonrightsService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ButtonService buttonService;
    @Autowired
    private YmlConfig ymlConfig;

    /*
     * @Author 西野
     * @Date 2018-09-17 11:14:51
     * @Description 按钮权限列表
     * @Param []
     * @Return com.xiye.common.entity.Result
     */
    @ApiOperation(value = "显示按钮权限列表", notes = "显示按钮权限列表")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "服务器异常")})
    @RequestMapping(value = "/listButtonRights", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public Result list(Page page) throws Exception {
        PageData pd = this.getPageData();
        page.setPageData(pd);
        Map<String, Object> map = new HashMap<String, Object>();
        String type = pd.getString("type");
        type = Tools.isEmpty(type) ? "0" : type;
        if (pd.getString("ROLE_ID") == null || "".equals(pd.getString("ROLE_ID").trim())) {
            pd.put("ROLE_ID", "1"); // 默认列出第一组角色(初始设计系统用户和会员组不能删除)
        }
        PageData fpd = new PageData();
        fpd.put("ROLE_ID", "0");
        List<Role> roleList = roleService.listAllRolesByPId(fpd); // 列出组(页面横向排列的一级组)
        //List<Role> roleList_z = roleService.listAllRolesByPId(pd); // 列出此组下架角色
        List<PageData> roleList_z = roleService.listAllRolesByPId(page); // 列出此组下架角色
        List<PageData> buttonlist = buttonService.listAll(pd); // 列出所有按钮
        List<PageData> rolebuttonlist = buttonrightsService.listAll(pd); // 列出所有角色按钮关联数据
        pd = roleService.findObjectById(pd); // 取得点击的角色组(横排的)
        map.put("pd", pd);
        map.put("Page", page);
        map.put("roleList", JSONArray.fromObject(roleList));
        map.put("roleList_z", JSONArray.fromObject(roleList_z));
        map.put("buttonlist", JSONArray.fromObject(buttonlist));
        map.put("rolebuttonlist", JSONArray.fromObject(rolebuttonlist));
        map.put("QX", Jurisdiction.getHC()); // 按钮权限
        return ResultUtil.setRowbackResut_entity(true, 200, ymlConfig.getServer_success(), map);
    }

    /*
     * @Author 西野
     * @Date 2018-09-17 11:19:54
     * @Description 点击按钮处理关联表
     * @Param []
     * @Return java.lang.Object
     */
    @ApiOperation(value = "按钮权限开关", notes = "修改按钮权限或者取消")
    @ApiImplicitParams({@ApiImplicitParam(name = "ROLE_ID", value = "角色ID", required = true), @ApiImplicitParam(name = "BUTTON_ID", value = "按钮ID", required = true)})
    @RequestMapping(value = "/upRoleButtonRights", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    public Result upRoleButtonRights() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
            return ResultUtil.setRowbackResut_entity(false, 403, ymlConfig.getAuthorization(), map);
        }
        PageData pd = this.getPageData();
        if (null != buttonrightsService.findById(pd)) { // 判断关联表是否有数据 是:删除/否:新增
            buttonrightsService.delete(pd); // 删除
        } else {
            pd.put("RB_ID", this.get32UUID()); // 主键
            buttonrightsService.save(pd); // 新增
        }
        return ResultUtil.setRowbackResut_entity(true, 200, ymlConfig.getServer_success(), map);
    }

}
